package com.itheima.controller;

import com.itheima.auth.CustomException;
import com.itheima.dto.MessageConstant;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.SetmealCheckgroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 编辑检查组
     *
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PutMapping
    public Result update(@RequestBody CheckGroup checkGroup, @RequestParam List<Integer> checkitemIds) {
        //更新checkGroup基本信息
        checkGroupService.update (checkGroup, checkitemIds);
        return Result.success ("编辑检查组成功");
    }

    /**
     * 查询所有的检查组数据【给下面套餐管理使用的】
     *
     * @return
     */
    @GetMapping("/list")
    public Result checkGroupList() {
        List<CheckGroup> checkGroupList = checkGroupService.checkGroupList ( );
        return Result.success (checkGroupList, "查询所有的检查组数据成功~");
    }

    /**
     * 根据检查组id查询对应的检查项id集合
     *
     * @param id
     * @return
     */
    @GetMapping("/checkItem/{id}")
    public Result checkItemByCheckGroupId(@PathVariable Integer id) {
        List<Integer> checkItemList = checkGroupService.checkItemByCheckGroupId (id);
        return Result.success (checkItemList, "查询成功~");
    }

    //条件查询检查组信息
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkGroupService.findPage (queryPageBean);
        return pageResult;
    }

    //新增检查组
    @PostMapping()
    public Result add(@RequestBody CheckGroup checkGroup) {
        checkGroupService.add (checkGroup);
        return Result.success (MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    //删除检查组
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        if (checkGroupService.selectById (id) > 0) {
            return Result.error (MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        checkGroupService.deleteById (id);
        return Result.success (MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 根据id查询检查组
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result chekDelect(@PathVariable Integer id) {
        CheckGroup checkGroup = checkGroupService.selectCheck (id);
        checkGroup.setId (id);
        return Result.success (checkGroup, "查询检查组成功");
    }


}
